//
//  LMBaseViewManager.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//

#import "THLiveRoomBasePopView.h"
@interface THLiveRoomBasePopView ()

@property (nonatomic, strong) UIView *maskLayerView;
@property (nonatomic, assign) CGRect contentSettedFrame;

@end

@implementation THLiveRoomBasePopView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self lmSetInitialValue];
        [self restPopViewInitialValue];
        [self setupUI];
        [self setupFrames];
    }
    return self;
}

- (void)restPopViewInitialValue{
    
}
- (void)setupUI{
    
}
- (void)setupFrames{
    
}
- (void)lmSetInitialValue{
    self.appearAnimationTime = 0.3;
    self.closeAnimationTime = 0.3;
    self.layer.masksToBounds = YES;
    self.removeWhenClosed = YES;
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
}
- (void)setMasklyerAlpha:(CGFloat)masklyerAlpha{
    if (_masklyerAlpha != masklyerAlpha) {
        _masklyerAlpha = masklyerAlpha;
        if (masklyerAlpha>0) {
            self.maskLayerView.alpha = masklyerAlpha;
            self.maskLayerView.backgroundColor = [UIColor blackColor];
        }else{//如果alpha设置为0就不能点击,所以设为透明色
            self.maskLayerView.alpha = 1;
            self.maskLayerView.backgroundColor = [UIColor clearColor];
        }
    }
}
- (void)setCanClose:(BOOL)canClose{
    _canClose = canClose;
    self.maskLayerView.userInteractionEnabled = canClose;
}
#pragma mark 显示当前视图的动画
- (void)showContentViewDone:(void(^)(void))done{//默认从底部出现 滑动到底部
    [self showContentViewFrom:ContentAppearFrom_Bottom to:ContentAppearTo_Bottom done:done];
}
- (void)showContentViewFrom:(ContentAppearFrom )fromWhere to:(ContentAppearTo)toWhere done:(void(^)(void))done {
    if (self.frame.size.width==0 && self.frame.size.height==0 && self.superview) {//如果未设置约束
        self.frame = self.superview.bounds;
    }
    if ((self.contentView.width!=self.width && self.contentView.centerX != self.centerX) || (self.contentView.height!=self.height && self.contentView.centerY!=self.centerY)) {
        [self layoutIfNeeded];
    }
    [self lmSetContentAppearFromFrameWithFromeWhere:fromWhere];
    [self lmSetAppearToFrameWithToWhere:toWhere done:done];
}
- (void)lmSetContentAppearFromFrameWithFromeWhere:(ContentAppearFrom)fromWhere{
    if (self.contentSettedFrame.size.width > 0 && self.contentSettedFrame.size.height > 0) {
        self.contentView.frame = self.contentSettedFrame;
    }
    CGFloat contentX = self.contentView.frame.origin.x, contentHeight = self.contentView.height, contentWidth = self.contentView.width;
//    contentY = self.contentView.frame.origin.y,
    CGFloat bgViewWidth = self.width, bgViewHeight = self.height;
    CGRect  contentFrame = CGRectZero;
    switch (fromWhere) {//从哪里来
        case ContentAppearFrom_Bottom:
        {
            contentFrame = CGRectMake(contentX, bgViewHeight, contentWidth, contentHeight);
        }
            break;
        case ContentAppearFrom_Top:
        {
            contentFrame = CGRectMake(contentX, -contentHeight, contentWidth, contentHeight);
        }
            break;
        case ContentAppearFrom_Center:
        {
            CGFloat contentCenterX = (bgViewWidth-contentWidth)/2, contentCenterY = (bgViewHeight-contentHeight)/2;
            contentFrame = CGRectMake(contentCenterX, contentCenterY, contentWidth, contentHeight);
        }
            break;
//        case ContentAppearFrom_Right:
//        {
//            contentFrame = CGRectMake(bgViewWidth, contentY, contentWidth, contentHeight);
//        }
//            break;
//        case ContentAppearFrom_Left:
//        {
//            contentFrame = CGRectMake(-contentWidth, contentY, contentWidth, contentHeight);
//        }
//            break;
        default:
            break;
    }
    self.contentView.frame = contentFrame;
    self.hidden = NO;
}
- (void)lmSetAppearToFrameWithToWhere:(ContentAppearTo)toWhere done:(void(^)(void))done{
    CGFloat contentX = self.contentView.frame.origin.x, contentHeight = self.contentView.height, contentWidth = self.contentView.width;
//    contentY = self.contentView.frame.origin.y,
    CGFloat bgViewWidth = self.width, bgViewHeight = self.height;
    CGFloat contentBottom = bgViewHeight - contentHeight;
//    contentRight = bgViewWidth-contentWidth,
    self.contentView.alpha = 1;
    CGRect contentFrame = self.contentView.frame;
       switch (toWhere) {//到哪里去
           case ContentAppearTo_Top:
           {
               contentFrame = CGRectMake(contentX, 0, contentWidth, contentHeight);
           }
               break;
           case ContentAppearTo_Bottom:
           {
               contentFrame = CGRectMake(contentX, contentBottom, contentWidth, contentHeight);
           }
               break;
           case ContentAppearTo_Center:
           {
               CGFloat contentCenterX = (bgViewWidth-contentWidth)/2, contentCenterY = (bgViewHeight-contentHeight)/2;
               contentFrame = CGRectMake(contentCenterX, contentCenterY, contentWidth, contentHeight);
           }
               break;
//           case ContentAppearTo_Left:
//           {
//               contentFrame = CGRectMake(0, contentY, contentWidth, contentHeight);
//           }
//               break;
//           case ContentAppearTo_Right:
//           {
//               contentFrame = CGRectMake(contentRight, contentY, contentWidth, contentHeight);
//           }
//               break;
           default:
               break;
       }
//    [[NSNotificationCenter defaultCenter] postNotificationName:TH_Noti_LiveRoom_Scrol_No object:self];
    [UIView animateWithDuration:self.appearAnimationTime animations:^{
        self.contentView.frame = contentFrame;
    } completion:^(BOOL finished) {
        if (done) {
            done();
        }
    }];
}
#pragma mark 隐藏当前视图的动画
- (void)closeContentViewDone:(void(^)(void))done {
    [self closeContentViewToWhere:ContentCloseTo_Bottom Done:done];
}

- (void)closeContentViewWithOutAnimation
{
    self.hidden = YES;
    [self.afterCloseSub sendNext:nil];
    if (self.removeWhenClosed) {
        [self removeFromSuperview];
    }
}

- (void)closeContentViewToWhere:(ContentCloseTo)toWhere Done:(void (^)(void))done{
    if (self.closeToFrame.origin.x>0 && self.closeToFrame.origin.y>0) {
        [UIView animateWithDuration:self.closeAnimationTime animations:^{
            self.contentView.frame = self.closeToFrame;
            self.contentView.alpha = 0;
        } completion:^(BOOL finished) {
            self.hidden = YES;
            if (done) {
                [self.afterCloseSub sendNext:nil];
                done();
            }
            if (self.removeWhenClosed) {
                [self removeFromSuperview];
            }
        }];
//        // 缩放动画
//        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
//        // 动画选项设定
//        animation.duration = self.closeAnimationTime; // 动画持续时间
//        // 缩放倍数
//        animation.fromValue = [NSNumber numberWithFloat:1.0]; // 开始时的倍率
//        animation.toValue = [NSNumber numberWithFloat:0.1]; // 结束时的倍率
//        // 添加动画
//        [self.contentView.layer addAnimation:animation forKey:@"scale-layer"];
    }else{
        CGFloat contentX = self.contentView.frame.origin.x, contentY = self.contentView.frame.origin.y, contentHeight = self.contentView.height, contentWidth = self.contentView.width;
        CGFloat bgViewWidth = self.width, bgViewHeight = self.height;
        CGRect contentFrame = self.contentView.frame;
        switch (toWhere) {
            case ContentCloseTo_Top:
            {
                contentFrame = CGRectMake(contentX, -contentHeight, contentWidth, contentHeight);
            }
                break;
            case ContentCloseTo_Bottom:
            {
                contentFrame = CGRectMake(contentX, bgViewHeight, contentWidth, contentHeight);
            }
                break;
            case ContentCloseTo_Left:
            {
                contentFrame = CGRectMake(-contentWidth, contentY, contentWidth, contentHeight);
            }
                break;
                
            case ContentCloseTo_Right:
            {
                contentFrame = CGRectMake(bgViewWidth, contentY, contentWidth, contentHeight);
            }
                break;
            case ContentCloseTo_Center:
            {
                CGFloat contentCenterX = (bgViewWidth-contentWidth)/2, contentCenterY = (bgViewHeight-contentHeight)/2;
                contentFrame = CGRectMake(contentCenterX, contentCenterY, contentWidth, contentHeight);
            }
                return;
            case ContentCloseTo_CurrentPosition:
            {
                self.hidden = YES;
            }
                return;
            default:
                break;
        }
        
        [UIView animateWithDuration:self.closeAnimationTime animations:^{
            self.contentView.frame = contentFrame;
        } completion:^(BOOL finished) {
            self.hidden = YES;
//            [[NSNotificationCenter defaultCenter] postNotificationName:TH_Noti_LiveRoom_Scrol_Yes object:self];
            if (done) {
                [self.afterCloseSub sendNext:nil];
                done();
            }
            if (self.removeWhenClosed) {
                [self removeFromSuperview];
            }
        }];
    }
}
- (void)closeContentView:(UITapGestureRecognizer *)tap{
    [self closeContentViewDone:^{}];
}

- (void)swipeCloseContentView:(UISwipeGestureRecognizer *)swipe
{
    
}

#pragma mark  懒加载
- (UIView *)maskLayerView{
    if (!_maskLayerView) {
        _maskLayerView = [[UIView alloc] init];
        _maskLayerView.backgroundColor = [UIColor clearColor];
        _maskLayerView.userInteractionEnabled = YES;//默认可点击
        [self insertSubview:_maskLayerView atIndex:0];
        [_maskLayerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.left.right.mas_equalTo(0);
        }];
        [_maskLayerView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeContentView:)]];
        [_maskLayerView addGestureRecognizer:[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeCloseContentView:)]];
    }
    return _maskLayerView;
}
- (UIView *)contentView{
    if (!_contentView) {
        _contentView = [[UIView alloc] init];
        [self insertSubview:_contentView aboveSubview:self.maskLayerView];
//        @weakify(self);
//        [_contentView setDidFinishAutoLayoutBlock:^(CGRect frame) {
//            @strongify(self);
//            if (self.contentSettedFrame.size.width == 0 || self.contentSettedFrame.size.height == 0) {
//                self.contentSettedFrame = frame;
//            }
//        }];
    }
    return _contentView;
}
- (RACSubject *)afterCloseSub{
    if (!_afterCloseSub) {
        _afterCloseSub = [RACSubject subject];
    }
    return _afterCloseSub;
}

@end
